package com.cn.wanxi.service;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.PATH;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务逻辑层
 * 在这里来封装数据，处理数据的逻辑关系
 */
public class CompanyService {


    /**
     * 封装数据
     * 这里面时不能直接存放数据
     * 逻辑关系
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
        /**
         * 1.创建记事本
         * 2.将数据存于记事本
         * 3.数据来源于记事本,读取
         * 4.封装数据到companyModel
         */
//        File file = IO.createNote(path, "company.txt");
        wirteNote(PATH.COMPANY_FILE_NAME.getPath());
        String result = IO.readNote();
        return setCompanyModel(result);
    }

    private void wirteNote(String fileName) {
        List<String> list = new ArrayList<>();
        CompanyDao companyDao = new CompanyDao();
        list.add(companyDao.getLogo());
        list.add(companyDao.getPhone());
        list.add(companyDao.getNet());
        list.add(companyDao.getEmail());
        list.add(companyDao.getLinePhone());
        list.add(companyDao.getAddress());
        list.add(companyDao.getVersion());
        IO.wirteNote(fileName, list);
    }

    private CompanyModel setCompanyModel(String result) {
        String[] results = result.split(PATH.SPLIT.getPath());
        for (String aa : results
        ) {
            System.out.println(aa);
        }
        CompanyModel companyModel = new CompanyModel();
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setNet(results[2]);
        companyModel.setEmail(results[3]);
        companyModel.setLinePhone(results[4]);
        companyModel.setAddress(results[5]);
        companyModel.setVersion(results[6]);

        return companyModel;
    }


}
